﻿using System;
using System.Collections.Generic;
using Engine;
using GameEntitySystem;
using TemplatesDatabase;
using Test1;

namespace Game
{
    public class ComponentYSChaseBehavior : ComponentBehavior, IUpdateable
    {
        private Dictionary<ModLoader, Action> Hooks = new Dictionary<ModLoader, Action>();

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemPlayers m_subsystemPlayers;

        public SubsystemSky m_subsystemSky;

        public SubsystemBodies m_subsystemBodies;

        public SubsystemTime m_subsystemTime;

        public SubsystemNoise m_subsystemNoise;

        public ComponentCreature m_componentCreature;

        public ComponentPathfinding m_componentPathfinding;

        public ComponentMiner m_componentMiner;

        public ComponentRandomFeedBehavior m_componentFeedBehavior;

        public ComponentCreatureModel m_componentCreatureModel;

        public DynamicArray<ComponentBody> m_componentBodies = new DynamicArray<ComponentBody>();

        public Random m_random = new Random();

        public new StateMachine m_stateMachine = new StateMachine();

        public float m_dayChaseRange;

        public float m_nightChaseRange;

        public float m_dayChaseTime;

        public float m_nightChaseTime;

        public float m_chaseNonPlayerProbability;

        public float m_chaseWhenAttackedProbability;

        public float m_chaseOnTouchProbability;

        public CreatureCategory m_autoChaseMask;

        public float m_importanceLevel;

        public float m_targetUnsuitableTime;

        public float m_targetInRangeTime;

        public double m_nextUpdateTime;

        public ComponentCreature m_target;

        public float m_dt;

        public float m_range;

        public float m_chaseTime;

        public bool m_isPersistent;

        public float m_autoChaseSuppressionTime;

        public double t_targetTime;

        public float t_aimTime;

        public Vector4 t_AimAtfMax;

        public Vector4 t_AimEndMax;

        public Vector3 f_aimFound;

        public Vector2 f_aimTimeMax;

        public ComponentCreature aim_target;

        public Vector2 StopRa;

        public SubsystemParticles mpar;

        public SubsystemExplosions mexp;

        public SubsystemAudio subsystemAudio;

        public ComponentCreature Target => m_target;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public override float ImportanceLevel => m_importanceLevel;

        public void Attack(ComponentCreature componentCreature, float maxRange, float maxChaseTime, bool isPersistent)
        {
            if (m_target == null)
            {
                t_targetTime = m_subsystemGameInfo.TotalElapsedGameTime;
            }

            m_target = componentCreature;
            m_nextUpdateTime = 0.0;
            m_range = maxRange;
            m_chaseTime = maxChaseTime;
            m_isPersistent = isPersistent;
            m_importanceLevel = 200f;
        }

        public void AttackA(ComponentCreature componentCreature, float maxRange, float maxChaseTime, bool isPersistent)
        {
            m_target = componentCreature;
            if (maxRange > m_range)
            {
                m_range = maxRange;
            }

            m_chaseTime = ((maxChaseTime > m_chaseTime) ? maxChaseTime : (m_chaseTime + maxChaseTime * 0.5f));
            m_isPersistent = isPersistent;
            m_importanceLevel = 200f;
        }

        public void Update(float dt)
        {
            m_autoChaseSuppressionTime -= dt;
            if (IsActive && m_target != null)
            {
                Vector3 position = m_componentCreature.ComponentBody.Position;
                float num = Vector3.Distance(position, m_target.ComponentBody.Position);
                bool flag = m_componentCreature.ComponentLocomotion.StunTime >= 0.5f;
                double totalElapsedGameTime = m_subsystemGameInfo.TotalElapsedGameTime;
                if (!flag && m_stateMachine.CurrentState != "Stuning")
                {
                    m_chaseTime -= dt;
                    m_componentCreature.ComponentCreatureModel.LookAtOrder = m_target.ComponentCreatureModel.EyePosition;
                    if (IsTargetInAttackRange(m_target.ComponentBody))
                    {
                        m_componentCreatureModel.AttackOrder = true;
                    }

                    if (m_componentCreatureModel.IsAttackHitMoment && (!(m_stateMachine.CurrentState == "瞬移背斩") || t_aimTime >= t_AimAtfMax.Y))
                    {
                        Vector3 hitPoint;
                        ComponentBody hitBody = GetHitBody(m_target.ComponentBody, out hitPoint);
                        if (hitBody != null)
                        {
                            float x = (m_isPersistent ? m_random.Float(8f, 10f) : 2f);
                            m_chaseTime = MathUtils.Max(m_chaseTime, x);
                            m_componentMiner.Hit(hitBody, hitPoint, m_componentCreature.ComponentBody.Matrix.Forward);
                            m_componentCreature.ComponentCreatureSounds.PlayAttackSound();
                        }
                    }
                }

                if (m_stateMachine.CurrentState == "毁灭冲击-冲刺中" && t_aimTime < f_aimTimeMax.X)
                {
                    Vector3 vector = Vector3.Normalize(f_aimFound);
                    m_componentCreature.ComponentBody.ApplyImpulse(vector * f_aimTimeMax.Y);
                    mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position, Vector3.UnitY));
                    mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position, Vector3.UnitY));
                    mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position, Vector3.UnitY));
                    m_componentCreature.ComponentBody.Rotation = Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationY(Vector2.Angle(vector.XZ, Vector2.Zero) + -(float)Math.PI / 2f));
                }

                Vector3 boxSize = m_componentCreature.ComponentBody.BoxSize;
                BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
                BoundingBox b = m_componentCreature.ComponentBody.BoundingBox;
                b.Min -= boxSize * 1.15f;
                b.Max += boxSize * 1.15f;
                foreach (ComponentBody body in m_subsystemBodies.Bodies)
                {
                    float num2 = Vector2.Distance(body.Position.XZ, position.XZ);
                    if (num2 > StopRa.X && num2 <= StopRa.X + StopRa.Y)
                    {
                        body.Velocity = Vector3.Normalize(position - body.Position);
                        mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, body.Position, Vector3.UnitY));
                    }

                    if (m_stateMachine.CurrentState == "毁灭冲击-冲刺中" && t_aimTime < f_aimTimeMax.X)
                    {
                        BoundingBox b2 = body.BoundingBox;
                        float num3 = ComponentBody.CalculateBoxBoxOverlap(ref b, ref b2, 0);
                        float num4 = ComponentBody.CalculateBoxBoxOverlap(ref b, ref b2, 1);
                        float num5 = ComponentBody.CalculateBoxBoxOverlap(ref b, ref b2, 2);
                        if (body.Entity != base.Entity && (num3 != 0f || num4 != 0f || num5 != 0f))
                        {
                            Vector3 vector2 = body.Position - position;
                            Vector3 hitPoint2 = position + vector2;
                            ComponentMiner.AttackBody(body, m_componentCreature, hitPoint2, vector2, m_componentMiner.AttackPower * m_random.Float(2f, 3f), isMeleeAttack: true);
                            body.ApplyImpulse(7.5f * Vector3.Normalize(vector2));
                        }
                    }
                }
            }

            if (m_subsystemTime.GameTime >= m_nextUpdateTime)
            {
                m_dt = m_random.Float(0.15f, 0.185f) + MathUtils.Min((float)(m_subsystemTime.GameTime - m_nextUpdateTime), 0.1f);
                m_nextUpdateTime = m_subsystemTime.GameTime + (double)m_dt;
                m_stateMachine.Update();
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemPlayers = base.Project.FindSubsystem<SubsystemPlayers>(throwOnError: true);
            m_subsystemSky = base.Project.FindSubsystem<SubsystemSky>(throwOnError: true);
            m_subsystemBodies = base.Project.FindSubsystem<SubsystemBodies>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemNoise = base.Project.FindSubsystem<SubsystemNoise>(throwOnError: true);
            mpar = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            mexp = base.Project.FindSubsystem<SubsystemExplosions>(throwOnError: true);
            subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_componentCreature = base.Entity.FindComponent<ComponentCreature>(throwOnError: true);
            m_componentPathfinding = base.Entity.FindComponent<ComponentPathfinding>(throwOnError: true);
            m_componentMiner = base.Entity.FindComponent<ComponentMiner>(throwOnError: true);
            m_componentFeedBehavior = base.Entity.FindComponent<ComponentRandomFeedBehavior>();
            m_componentCreatureModel = base.Entity.FindComponent<ComponentCreatureModel>(throwOnError: true);
            m_dayChaseRange = valuesDictionary.GetValue<float>("DayChaseRange");
            m_nightChaseRange = valuesDictionary.GetValue<float>("NightChaseRange");
            m_dayChaseTime = valuesDictionary.GetValue<float>("DayChaseTime");
            m_nightChaseTime = valuesDictionary.GetValue<float>("NightChaseTime");
            m_autoChaseMask = valuesDictionary.GetValue<CreatureCategory>("AutoChaseMask");
            m_chaseNonPlayerProbability = valuesDictionary.GetValue<float>("ChaseNonPlayerProbability");
            m_chaseWhenAttackedProbability = valuesDictionary.GetValue<float>("ChaseWhenAttackedProbability");
            m_chaseOnTouchProbability = valuesDictionary.GetValue<float>("ChaseOnTouchProbability");
            t_AimAtfMax = valuesDictionary.GetValue("AimAtfMax", new Vector4(1.25f, 0.35f, 1.5f, 5f));
            f_aimTimeMax = valuesDictionary.GetValue("AimRunMax", (Vector2)(1.3f, 3f));
            t_AimEndMax = valuesDictionary.GetValue("AimEndMax", new Vector4(0.5f, 3f, 2f, 2f));
            StopRa = valuesDictionary.GetValue("StopRa", (Vector2)(16f, 4.5f));
            ComponentBody componentBody = m_componentCreature.ComponentBody;
            componentBody.CollidedWithBody = (Action<ComponentBody>)Delegate.Combine(componentBody.CollidedWithBody, (Action<ComponentBody>)delegate (ComponentBody body)
            {
                if (m_target == null && m_autoChaseSuppressionTime <= 0f && m_random.Float(0f, 1f) < m_chaseOnTouchProbability)
                {
                    ComponentCreature componentCreature2 = body.Entity.FindComponent<ComponentCreature>();
                    if (componentCreature2 != null)
                    {
                        bool flag3 = m_subsystemPlayers.IsPlayer(body.Entity);
                        bool flag4 = (componentCreature2.Category & m_autoChaseMask) != 0;
                        if ((flag3 && m_subsystemGameInfo.WorldSettings.GameMode > GameMode.Harmless) || (!flag3 && flag4))
                        {
                            Attack(componentCreature2, 7f, 7f, isPersistent: false);
                        }
                    }
                }

                double totalElapsedGameTime2 = m_subsystemGameInfo.TotalElapsedGameTime;
                if (!(m_stateMachine.CurrentState == "Stuning") && m_target != null && body == m_target.ComponentBody && body.StandingOnBody == m_componentCreature.ComponentBody)
                {
                    m_componentCreature.ComponentLocomotion.JumpOrder = 1f;
                    m_componentCreatureModel.AttackOrder = true;
                }
            });
            m_componentCreature.ComponentHealth.Attacked += delegate (ComponentCreature attacker)
            {
                if (m_random.Float(0f, 1f) < m_chaseWhenAttackedProbability)
                {
                    if (m_chaseWhenAttackedProbability >= 1f)
                    {
                        Attack(attacker, 30f, 60f, isPersistent: true);
                    }
                    else
                    {
                        Attack(attacker, 7f, 7f, isPersistent: false);
                    }
                }
            };
            m_stateMachine.AddState("LookingForTarget", delegate
            {
                m_importanceLevel = 0f;
                m_target = null;
            }, delegate
            {
                if (IsActive)
                {
                    m_stateMachine.TransitionTo("Chasing");
                }
                else if (m_autoChaseSuppressionTime <= 0f && (m_target == null || ScoreTarget(m_target) <= 0f) && m_componentCreature.ComponentHealth.Health > 0.4f)
                {
                    m_range = ((m_subsystemSky.SkyLightIntensity < 0.2f) ? m_nightChaseRange : m_dayChaseRange);
                    ComponentCreature componentCreature = FindTarget();
                    if (componentCreature != null)
                    {
                        m_targetInRangeTime += m_dt;
                    }
                    else
                    {
                        m_targetInRangeTime = 0f;
                    }

                    if (m_targetInRangeTime > 3f)
                    {
                        bool flag2 = m_subsystemSky.SkyLightIntensity >= 0.1f;
                        float maxRange = (flag2 ? (m_dayChaseRange + 6f) : (m_nightChaseRange + 6f));
                        float maxChaseTime = (flag2 ? (m_dayChaseTime * m_random.Float(0.75f, 1f)) : (m_nightChaseTime * m_random.Float(0.75f, 1f)));
                        Attack(componentCreature, maxRange, maxChaseTime, (!flag2) ? true : false);
                    }
                }
            }, null);
            m_stateMachine.AddState("RandomMoving", delegate
            {
                m_componentPathfinding.SetDestination(m_componentCreature.ComponentBody.Position + new Vector3(6f * m_random.Float(-1f, 1f), 0f, 6f * m_random.Float(-1f, 1f)), 1f, 1f, 0, useRandomMovements: false, ignoreHeightDifference: true, raycastDestination: false, null);
            }, delegate
            {
                if (m_componentPathfinding.IsStuck || !m_componentPathfinding.Destination.HasValue)
                {
                    m_stateMachine.TransitionTo("Chasing");
                }

                if (!IsActive)
                {
                    m_stateMachine.TransitionTo("LookingForTarget");
                }
            }, delegate
            {
                m_componentPathfinding.Stop();
            });
            m_stateMachine.AddState("Chasing", delegate
            {
                m_subsystemNoise.MakeNoise(m_componentCreature.ComponentBody, 0.25f, 6f);
                m_componentCreature.ComponentCreatureSounds.PlayIdleSound(skipIfRecentlyPlayed: false);
                m_nextUpdateTime = 0.0;
                t_aimTime = 0f;
            }, delegate
            {
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    if (ScoreTarget(m_target) <= 0f)
                    {
                        m_targetUnsuitableTime += m_dt;
                    }
                    else
                    {
                        m_targetUnsuitableTime = 0f;
                    }

                    double totalElapsedGameTime = m_subsystemGameInfo.TotalElapsedGameTime;
                    if (m_targetUnsuitableTime > 8f)
                    {
                        m_importanceLevel = 0f;
                    }
                    else
                    {
                        Vector3 position8 = m_componentCreature.ComponentBody.Position;
                        t_aimTime += m_dt;
                        float num11 = Vector3.Distance(position8, m_target.ComponentBody.Position);
                        if (num11 <= 10.5f)
                        {
                            subsystemAudio.PlaySound("Audio/Creatures/YS/YSlaugh", 1f, 0f, 0f, 0f);
                            m_stateMachine.TransitionTo("毁灭冲击");
                        }
                        else if (t_aimTime >= t_AimEndMax.X / 2f)
                        {
                            m_stateMachine.TransitionTo("瞬移背斩");
                            subsystemAudio.PlaySound("Audio/Creatures/YS/YSlaugh", 1f, 0f, 0f, 0f);
                        }
                        else
                        {
                            int maxPathfindingPositions4 = 0;
                            if (m_isPersistent)
                            {
                                maxPathfindingPositions4 = (m_subsystemTime.FixedTimeStep.HasValue ? 1500 : 500);
                            }

                            BoundingBox boundingBox9 = m_componentCreature.ComponentBody.BoundingBox;
                            BoundingBox boundingBox10 = m_target.ComponentBody.BoundingBox;
                            Vector3 v4 = 0.5f * (boundingBox9.Min + boundingBox9.Max);
                            Vector3 vector6 = 0.5f * (boundingBox10.Min + boundingBox10.Max);
                            float num12 = Vector3.Distance(v4, vector6);
                            float num13 = ((num12 < 4f) ? 0.2f : 0f);
                            float speed4 = 1f;
                            m_componentPathfinding.SetDestination(vector6 + num13 * num12 * m_target.ComponentBody.Velocity, speed4, 1.5f, maxPathfindingPositions4, useRandomMovements: true, ignoreHeightDifference: false, raycastDestination: true, m_target.ComponentBody);
                            if (m_random.Float(0f, 1f) < 0.33f * m_dt)
                            {
                                m_componentCreature.ComponentCreatureSounds.PlayAttackSound();
                            }
                        }
                    }
                }
            }, null);
            m_stateMachine.AddState("毁灭冲击", delegate
            {
                m_componentPathfinding.Stop();
                t_aimTime = 0f;
                Vector3 position7 = m_componentCreature.ComponentBody.Position;
                mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position7, Vector3.UnitY));
                mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position7, Vector3.UnitY));
                mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position7, Vector3.UnitY));
                mpar.AddParticleSystem(new GunSmokeParticleSystem(m_subsystemSky.m_subsystemTerrain, position7, Vector3.UnitY));
            }, delegate
            {
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    t_aimTime += m_dt;
                    Vector3 position6 = m_componentCreature.ComponentBody.Position;
                    if (t_aimTime <= 0.5f)
                    {
                        f_aimFound = m_target.ComponentBody.Position - position6;
                        m_componentCreature.ComponentBody.Rotation = Quaternion.CreateFromRotationMatrix(Matrix.CreateRotationY(Vector2.Angle(f_aimFound.XZ, Vector2.Zero) + -(float)Math.PI / 2f));
                    }

                    if (t_aimTime >= t_AimAtfMax.X)
                    {
                        m_stateMachine.TransitionTo("毁灭冲击-冲刺中");
                        Random random = new Random();
                        string[] array = new string[2] { "Audio/Creatures/YS/YSword", "Audio/Creatures/YS/YSRun" };
                        subsystemAudio.PlaySound(array[random.Int(0, 1)], 1f, 0f, 0f, 0f);
                    }
                }
            }, delegate
            {
                m_componentPathfinding.Stop();
            });
            m_stateMachine.AddState("毁灭冲击-冲刺中", delegate
            {
                m_componentPathfinding.Stop();
                t_aimTime = 0f;
            }, delegate
            {
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    if (t_aimTime >= f_aimTimeMax.X + t_AimEndMax.X)
                    {
                        m_stateMachine.TransitionTo("瞬移背斩");
                    }
                    else
                    {
                        t_aimTime += m_dt;
                        Vector3 position5 = m_componentCreature.ComponentBody.Position;
                        if (t_aimTime < f_aimTimeMax.X)
                        {
                            Vector3 vector5 = Vector3.Normalize(f_aimFound);
                        }
                    }
                }
            }, delegate
            {
                m_componentPathfinding.Stop();
                m_componentCreature.ComponentBody.Velocity = Vector3.Zero;
            });
            m_stateMachine.AddState("瞬移背斩", delegate
            {
                subsystemAudio.PlaySound("Audio/Creatures/YS/YSTp", 2f, 0f, 0f, 0f);
                m_componentCreature.ComponentBody.Velocity = Vector3.Zero;
                m_componentCreature.ComponentBody.m_velocity = Vector3.Zero;
                m_componentPathfinding.Stop();
                t_aimTime = 0f;
                Vector3 position2 = m_componentCreature.ComponentBody.Position;
                BoundingBox boundingBox7 = m_componentCreature.ComponentBody.BoundingBox;
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    Vector3 position3 = m_target.ComponentBody.Position;
                    Matrix matrix = m_target.ComponentBody.Matrix;
                    BoundingBox boundingBox8 = m_target.ComponentBody.BoundingBox;
                    mpar.AddParticleSystem(new KillParticleSystem(m_subsystemSky.m_subsystemTerrain, boundingBox7.Center(), 1.24f));
                    mpar.AddParticleSystem(new KillParticleSystem(m_subsystemSky.m_subsystemTerrain, boundingBox7.Center(), 1.24f));
                    mpar.AddParticleSystem(new KillParticleSystem(m_subsystemSky.m_subsystemTerrain, boundingBox7.Center(), 1.24f));
                    Vector3 position4 = position3 + matrix.Forward * -1.1f * boundingBox8.Size().X + Vector3.UnitY * 0.1f;
                    m_componentCreature.ComponentBody.Position = position4;
                    m_componentCreature.ComponentBody.Rotation = Quaternion.CreateFromRotationMatrix(matrix);
                    boundingBox7 = m_componentCreature.ComponentBody.BoundingBox;
                    mpar.AddParticleSystem(new KillParticleSystem(m_subsystemSky.m_subsystemTerrain, boundingBox7.Center(), 1.24f));
                    mpar.AddParticleSystem(new KillParticleSystem(m_subsystemSky.m_subsystemTerrain, boundingBox7.Center(), 1.24f));
                    mpar.AddParticleSystem(new KillParticleSystem(m_subsystemSky.m_subsystemTerrain, boundingBox7.Center(), 1.24f));
                }
            }, delegate
            {
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    t_aimTime += m_dt;
                    if (t_aimTime >= t_AimAtfMax.Y)
                    {
                        if (t_aimTime - m_dt < t_AimAtfMax.Y)
                        {
                            m_componentCreatureModel.AttackOrder = true;
                        }

                        if (t_aimTime >= t_AimAtfMax.Y + t_AimEndMax.Y)
                        {
                            subsystemAudio.PlaySound("Audio/Creatures/YS/YSexploded", 1f, 0f, 0f, 0f);
                            m_stateMachine.TransitionTo("原地爆炸");
                        }
                        else
                        {
                            int maxPathfindingPositions3 = 0;
                            if (m_isPersistent)
                            {
                                maxPathfindingPositions3 = (m_subsystemTime.FixedTimeStep.HasValue ? 1500 : 500);
                            }

                            BoundingBox boundingBox5 = m_componentCreature.ComponentBody.BoundingBox;
                            BoundingBox boundingBox6 = m_target.ComponentBody.BoundingBox;
                            Vector3 v3 = 0.5f * (boundingBox5.Min + boundingBox5.Max);
                            Vector3 vector4 = 0.5f * (boundingBox6.Min + boundingBox6.Max);
                            float num9 = Vector3.Distance(v3, vector4);
                            float num10 = ((num9 < 4f) ? 0.2f : 0f);
                            float speed3 = 1f;
                            m_componentPathfinding.SetDestination(vector4 + num10 * num9 * m_target.ComponentBody.Velocity, speed3, 1.5f, maxPathfindingPositions3, useRandomMovements: true, ignoreHeightDifference: false, raycastDestination: true, m_target.ComponentBody);
                            if (m_random.Float(0f, 1f) < 0.33f * m_dt)
                            {
                                m_componentCreature.ComponentCreatureSounds.PlayAttackSound();
                            }
                        }
                    }
                }
            }, delegate
            {
                m_componentPathfinding.Stop();
            });
            m_stateMachine.AddState("原地爆炸", delegate
            {
                m_componentPathfinding.Stop();
                t_aimTime = 0f;
            }, delegate
            {
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    t_aimTime += m_dt;
                    Vector3 position = m_componentCreature.ComponentBody.Position;
                    if (t_aimTime >= t_AimAtfMax.Z)
                    {
                        if (t_aimTime - m_dt < t_AimAtfMax.Z)
                        {
                            Point3 point = Terrain.ToCell(position);
                            int num3 = 0;
                            int num4 = 2;
                            for (int i = 0; i < 4; i++)
                            {
                                int num5 = i;
                                for (int j = 0; j <= num4 * 2; j++)
                                {
                                    for (int k = 0; k <= num4; k++)
                                    {
                                        for (int l = 0; l <= num4; l++)
                                        {
                                            Point3 point2 = new Point3(point.X + k * ((num5 != 1 && num5 != 2) ? 1 : (-1)), point.Y + j, point.Z + l * ((num5 != 1 && num5 != 3) ? 1 : (-1)));
                                            int cellValue = m_subsystemSky.m_subsystemTerrain.Terrain.GetCellValue(point2.X, point2.Y, point2.Z);
                                            int num6 = Terrain.ExtractContents(cellValue);
                                            Block block = BlocksManager.Blocks[num6];
                                            if (num6 != 1)
                                            {
                                                bool flag = num3 < 1200;
                                                m_subsystemSky.m_subsystemTerrain.DestroyCell(0, point2.X, point2.Y, point2.Z, 0, !flag, noParticleSystem: true);
                                                if (flag)
                                                {
                                                    num3++;
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            mexp.AddExplosion(point.X, point.Y, point.Z, 15f, isIncendiary: true, noExplosionSound: false);
                            Vector3 vector2 = new Vector3(point.X, point.Y, point.Z);
                        }

                        if (t_aimTime >= t_AimAtfMax.Z + t_AimEndMax.Z)
                        {
                            m_stateMachine.TransitionTo("恐惧凝视");
                        }
                        else
                        {
                            int maxPathfindingPositions2 = 0;
                            if (m_isPersistent)
                            {
                                maxPathfindingPositions2 = (m_subsystemTime.FixedTimeStep.HasValue ? 1500 : 500);
                            }

                            BoundingBox boundingBox3 = m_componentCreature.ComponentBody.BoundingBox;
                            BoundingBox boundingBox4 = m_target.ComponentBody.BoundingBox;
                            Vector3 v2 = 0.5f * (boundingBox3.Min + boundingBox3.Max);
                            Vector3 vector3 = 0.5f * (boundingBox4.Min + boundingBox4.Max);
                            float num7 = Vector3.Distance(v2, vector3);
                            float num8 = ((num7 < 4f) ? 0.2f : 0f);
                            float speed2 = 1f;
                            m_componentPathfinding.SetDestination(vector3 + num8 * num7 * m_target.ComponentBody.Velocity, speed2, 1.5f, maxPathfindingPositions2, useRandomMovements: true, ignoreHeightDifference: false, raycastDestination: true, m_target.ComponentBody);
                            if (m_random.Float(0f, 1f) < 0.33f * m_dt)
                            {
                                m_componentCreature.ComponentCreatureSounds.PlayAttackSound();
                            }
                        }
                    }
                }
            }, delegate
            {
                m_componentPathfinding.Stop();
            });
            m_stateMachine.AddState("恐惧凝视", delegate
            {
                m_componentPathfinding.Stop();
                t_aimTime = 0f;
                aim_target = m_target;
            }, delegate
            {
                if (!(m_componentCreature.ComponentLocomotion.StunTime >= 0.5f) && Chase取消追击检测())
                {
                    t_aimTime += m_dt;
                    Vector3 eyePosition = m_componentCreature.ComponentCreatureModel.EyePosition;
                    if (t_aimTime >= t_AimAtfMax.W)
                    {
                        if (t_aimTime - m_dt < t_AimAtfMax.W)
                        {
                            if (aim_target == m_target)
                            {
                                ComponentTest1 componentTest = aim_target.Entity.FindComponent<ComponentTest1>();
                                if (componentTest != null)
                                {
                                    componentTest.m_sen -= 30f;
                                    ComponentPlayer componentPlayer = componentTest.Entity.FindComponent<ComponentPlayer>();
                                    componentPlayer.ComponentGui.DisplayLargeMessage("隐士对你使用了噩梦凝视！SEN-30！", null, 2.5f, 0f);
                                }
                            }
                            else
                            {
                                m_componentCreature.ComponentLocomotion.StunTime += 5f;
                            }
                        }

                        if (t_aimTime >= t_AimAtfMax.W + t_AimEndMax.W)
                        {
                            m_stateMachine.TransitionTo("Chasing");
                        }
                    }
                    else
                    {
                        mpar.AddParticleSystem(new WaterSplashParticleSystem(m_subsystemSky.m_subsystemTerrain, eyePosition + m_random.Vector3(0.7f), large: true));
                        mpar.AddParticleSystem(new WaterSplashParticleSystem(m_subsystemSky.m_subsystemTerrain, eyePosition + m_random.Vector3(0.7f), large: true));
                        mpar.AddParticleSystem(new WaterSplashParticleSystem(m_subsystemSky.m_subsystemTerrain, eyePosition + m_random.Vector3(0.7f), large: true));
                    }

                    int maxPathfindingPositions = 0;
                    if (m_isPersistent)
                    {
                        maxPathfindingPositions = (m_subsystemTime.FixedTimeStep.HasValue ? 1500 : 500);
                    }

                    BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
                    BoundingBox boundingBox2 = m_target.ComponentBody.BoundingBox;
                    Vector3 v = 0.5f * (boundingBox.Min + boundingBox.Max);
                    Vector3 vector = 0.5f * (boundingBox2.Min + boundingBox2.Max);
                    float num = Vector3.Distance(v, vector);
                    float num2 = ((num < 4f) ? 0.2f : 0f);
                    float speed = 1f;
                    m_componentPathfinding.SetDestination(vector + num2 * num * m_target.ComponentBody.Velocity, speed, 1.5f, maxPathfindingPositions, useRandomMovements: true, ignoreHeightDifference: false, raycastDestination: true, m_target.ComponentBody);
                    if (m_random.Float(0f, 1f) < 0.33f * m_dt)
                    {
                        m_componentCreature.ComponentCreatureSounds.PlayAttackSound();
                    }
                }
            }, delegate
            {
                m_componentPathfinding.Stop();
            });
            m_stateMachine.TransitionTo("LookingForTarget");
        }

        public bool Chase取消追击检测()
        {
            if (!IsActive)
            {
                m_stateMachine.TransitionTo("LookingForTarget");
            }
            else if (m_chaseTime <= 0f)
            {
                m_autoChaseSuppressionTime = m_random.Float(10f, 60f);
                m_importanceLevel = 0f;
            }
            else if (m_target == null)
            {
                m_importanceLevel = 0f;
            }
            else if (m_target.ComponentHealth.Health <= 0f)
            {
                if (m_componentFeedBehavior != null)
                {
                    m_subsystemTime.QueueGameTimeDelayedExecution(m_subsystemTime.GameTime + (double)m_random.Float(1f, 3f), delegate
                    {
                        if (m_target != null)
                        {
                            m_componentFeedBehavior.Feed(m_target.ComponentBody.Position);
                        }
                    });
                }

                m_importanceLevel = 0f;
            }
            else if (!m_isPersistent && m_componentPathfinding.IsStuck)
            {
                m_importanceLevel = 0f;
            }
            else
            {
                if (!m_isPersistent || !m_componentPathfinding.IsStuck)
                {
                    return true;
                }

                m_stateMachine.TransitionTo("RandomMoving");
            }

            return false;
        }

        public ComponentCreature FindTarget()
        {
            Vector3 position = m_componentCreature.ComponentBody.Position;
            ComponentCreature result = null;
            float num = 0f;
            m_componentBodies.Clear();
            m_subsystemBodies.FindBodiesAroundPoint(new Vector2(position.X, position.Z), m_range, m_componentBodies);
            for (int i = 0; i < m_componentBodies.Count; i++)
            {
                ComponentCreature componentCreature = m_componentBodies.Array[i].Entity.FindComponent<ComponentCreature>();
                if (componentCreature != null)
                {
                    float num2 = ScoreTarget(componentCreature);
                    if (num2 > num)
                    {
                        num = num2;
                        result = componentCreature;
                    }
                }
            }

            return result;
        }

        public float ScoreTarget(ComponentCreature componentCreature)
        {
            bool flag = componentCreature.Entity.FindComponent<ComponentPlayer>() != null;
            bool flag2 = m_componentCreature.Category != CreatureCategory.WaterPredator && m_componentCreature.Category != CreatureCategory.WaterOther;
            bool flag3 = componentCreature == Target || m_subsystemGameInfo.WorldSettings.GameMode > GameMode.Harmless;
            bool flag4 = (componentCreature.Category & m_autoChaseMask) != 0;
            bool flag5 = componentCreature == Target || (flag4 && MathUtils.Remainder(0.004999999888241291 * m_subsystemTime.GameTime + (double)((float)(GetHashCode() % 1000) / 1000f) + (double)((float)(componentCreature.GetHashCode() % 1000) / 1000f), 1.0) < (double)m_chaseNonPlayerProbability);
            if (componentCreature != m_componentCreature && ((!flag && flag5) || (flag && flag3)) && componentCreature.Entity.IsAddedToProject && componentCreature.ComponentHealth.Health > 0f && (flag2 || IsTargetInWater(componentCreature.ComponentBody)))
            {
                float num = Vector3.Distance(m_componentCreature.ComponentBody.Position, componentCreature.ComponentBody.Position);
                if (num < m_range)
                {
                    return m_range - num;
                }
            }

            return 0f;
        }

        public bool IsTargetInWater(ComponentBody target)
        {
            if (target.ImmersionDepth > 0f)
            {
                return true;
            }

            if (target.ParentBody != null && IsTargetInWater(target.ParentBody))
            {
                return true;
            }

            if (target.StandingOnBody != null && target.StandingOnBody.Position.Y < target.Position.Y && IsTargetInWater(target.StandingOnBody))
            {
                return true;
            }

            return false;
        }

        public bool IsTargetInAttackRange(ComponentBody target)
        {
            if (IsBodyInAttackRange(target))
            {
                return true;
            }

            BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
            BoundingBox boundingBox2 = target.BoundingBox;
            Vector3 vector = 0.5f * (boundingBox.Min + boundingBox.Max);
            Vector3 vector2 = 0.5f * (boundingBox2.Min + boundingBox2.Max) - vector;
            float num = vector2.Length();
            Vector3 v = vector2 / num;
            float num2 = 0.5f * (boundingBox.Max.X - boundingBox.Min.X + boundingBox2.Max.X - boundingBox2.Min.X);
            float num3 = 0.5f * (boundingBox.Max.Y - boundingBox.Min.Y + boundingBox2.Max.Y - boundingBox2.Min.Y);
            if (MathUtils.Abs(vector2.Y) < num3 * 0.99f)
            {
                if (num < num2 + 0.99f && Vector3.Dot(v, m_componentCreature.ComponentBody.Matrix.Forward) > 0.25f)
                {
                    return true;
                }
            }
            else if (num < num3 + 0.3f && MathUtils.Abs(Vector3.Dot(v, Vector3.UnitY)) > 0.8f)
            {
                return true;
            }

            if (target.ParentBody != null && IsTargetInAttackRange(target.ParentBody))
            {
                return true;
            }

            if (target.StandingOnBody != null && target.StandingOnBody.Position.Y < target.Position.Y && IsTargetInAttackRange(target.StandingOnBody))
            {
                return true;
            }

            return false;
        }

        public bool IsBodyInAttackRange(ComponentBody target)
        {
            BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
            BoundingBox boundingBox2 = target.BoundingBox;
            Vector3 vector = 0.5f * (boundingBox.Min + boundingBox.Max);
            Vector3 vector2 = 0.5f * (boundingBox2.Min + boundingBox2.Max) - vector;
            float num = vector2.Length();
            Vector3 v = vector2 / num;
            float num2 = 0.5f * (boundingBox.Max.X - boundingBox.Min.X + boundingBox2.Max.X - boundingBox2.Min.X);
            float num3 = 0.5f * (boundingBox.Max.Y - boundingBox.Min.Y + boundingBox2.Max.Y - boundingBox2.Min.Y);
            if (MathUtils.Abs(vector2.Y) < num3 * 0.99f)
            {
                if (num < num2 + 0.99f && Vector3.Dot(v, m_componentCreature.ComponentBody.Matrix.Forward) > 0.25f)
                {
                    return true;
                }
            }
            else if (num < num3 + 0.3f && MathUtils.Abs(Vector3.Dot(v, Vector3.UnitY)) > 0.8f)
            {
                return true;
            }

            return false;
        }

        public ComponentBody GetHitBody(ComponentBody target, out Vector3 hitPoint)
        {
            Vector3 vector = m_componentCreature.ComponentBody.BoundingBox.Center();
            Vector3 vector2 = target.BoundingBox.Center();
            Ray3 ray = new Ray3(vector, Vector3.Normalize(vector2 - vector));
            BodyRaycastResult? bodyRaycastResult = m_componentMiner.Raycast<BodyRaycastResult>(ray, RaycastMode.Interaction);
            if (bodyRaycastResult.HasValue && bodyRaycastResult.Value.Distance < 1.75f && (bodyRaycastResult.Value.ComponentBody == target || bodyRaycastResult.Value.ComponentBody.IsChildOfBody(target) || target.IsChildOfBody(bodyRaycastResult.Value.ComponentBody) || target.StandingOnBody == bodyRaycastResult.Value.ComponentBody))
            {
                hitPoint = bodyRaycastResult.Value.HitPoint();
                return bodyRaycastResult.Value.ComponentBody;
            }

            hitPoint = default(Vector3);
            return null;
        }
    }
}